package com.yubest;

/**
 * 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性：
 *
 * 每行的元素从左到右升序排列。
 * 每列的元素从上到下升序排列。
 *  
 *
 * 示例 1：[图片] img/0240_1.jpg
 *
 *
 * 输入：matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
 * 输出：true
 * 示例 2：[图片] img/0240_2.jpg
 *
 *
 * 输入：matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
 * 输出：false
 *  
 *
 * 提示：
 *
 * m == matrix.length
 * n == matrix[i].length
 * 1 <= n, m <= 300
 * -10^9 <= matrix[i][j] <= 10^9
 * 每行的所有元素从左到右升序排列
 * 每列的所有元素从上到下升序排列
 * -10^9 <= target <= 10^9
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/search-a-2d-matrix-ii
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 *
 * @Author hweiyu
 * @Description
 * @Date 2021/10/25 8:54
 */
public class P0240 {

    public static void main(String[] args) {
        int[][] matrix = {{1, 4, 7, 11, 15}, {2, 5, 8, 12, 19}, {3, 6, 9, 16, 22}, {10, 13, 14, 17, 24}, {18, 21, 23, 26, 30}};
        System.out.println(new Solution240().searchMatrix(matrix, 5));

        int[][] matrix2 = {{1, 4, 7, 11, 15}, {2, 5, 8, 12, 19}, {3, 6, 9, 16, 22}, {10, 13, 14, 17, 24}, {18, 21, 23, 26, 30}};
        System.out.println(new Solution240().searchMatrix(matrix2, 20));

        int[][] matrix3 = {{1, 4, 7, 11, 15}, {2, 5, 8, 12, 19}, {3, 6, 9, 16, 22}, {10, 13, 14, 17, 24}, {18, 21, 23, 26, 30}};
        System.out.println(new Solution240().searchMatrixZ(matrix3, 20));

    }
}

class Solution240 {

    /**
     * 思路：二分查找，先定位到每一行的中间数，然后分别向两边查询比较
     * @param matrix
     * @param target
     * @return
     */
    public boolean searchMatrix(int[][] matrix, int target) {
        for (int i = 0; i < matrix.length; i++) {
            if (matrix[i][0] < target) {
                if (search(matrix[i], target, 0, matrix[i].length - 1)) {
                    return true;
                }
            } else {
                return matrix[i][0] == target;
            }
        }
        return false;
    }

    private boolean search(int[] arr, int target, int start, int end) {
        if (start > end) {
            return false;
        }
        int p = start + (end - start) / 2;
        if (arr[p] == target) {
            return true;
        }
        if (arr[p] < target) {
            return search(arr, target, p + 1, end);
        } else {
            return search(arr, target, start, p - 1);
        }
    }

    /**
     * 思路：Z字查找，从右上角开始，往左下角进行，每次可以排除一行或一列
     * @param matrix
     * @param target
     * @return
     */
    public boolean searchMatrixZ(int[][] matrix, int target) {
        //右上角元素对应的坐标
        int row = matrix[0].length - 1;
        int col = 0;
        while (row >= 0 && col < matrix.length) {
            if (matrix[col][row] == target) {
                return true;
            } else if (matrix[col][row] < target) {
                col++;
            } else if (matrix[col][row] > target) {
                row--;
            }
        }
        return false;
    }

}
